package com.violet.uaa.server.login.entity;

import com.violet.auth.client.token.CustomWebAuthenticationDetails;
import com.violet.common.constant.universal.SecurityConstants;
import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 类说明: 对表单登录的认证信息对象信息进行补充
 * AuthenticationDetailsSource该接口用于在SpringSecurity登录过程中对用户的登录信息的详细信息进行填充，
 * 默认实现是WebAuthenticationDetailsSource，生成上面的默认实现WebAuthenticationDetails。
 * 我们编写类实现AuthenticationDetailsSource，用于生成上面自定义的CustomWebAuthenticationDetails。
 *
 * @author wqf
 * @date 2024/9/3 11:42
 */
@Component
public class CustomAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, CustomWebAuthenticationDetails> {
    @Override
    public CustomWebAuthenticationDetails buildDetails(HttpServletRequest context) {
        String remoteAddress = context.getRemoteAddr();
        HttpSession session = context.getSession(false);
        String accountType = context.getHeader(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME);
        String sessionId = session != null ? session.getId() : null;
        return new CustomWebAuthenticationDetails(remoteAddress, sessionId, accountType);
    }
}
